package com.course.business.controller.admin;

import com.course.server.dto.CategoryDto;
import com.course.server.dto.PageDto;
import com.course.server.dto.ResponseDto;
import com.course.server.service.CategoryService;
import com.course.server.utils.ValidatorUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 〈一句话功能简述>
 * 〈〉
 *
 * @Author metype
 * @Create 2021/5/25
 * @Since 1.0.0
 */
@RestController
@RequestMapping("/admin/category")
@Api("分类管理")
public class CategoryController {

    private static final Logger LOG = LoggerFactory.getLogger(CategoryController.class);
    public static final String BUSINESS_NAME = "分类";

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    @ApiOperation("获取系统类型")
    @ApiImplicitParam(name = "PageDto<CategoryDto>", value = "请求信息", required = true, dataType = "object", paramType = "json")
    public ResponseDto <PageDto<CategoryDto>> test(@RequestBody PageDto<CategoryDto> pageDto) {
        LOG.info("pageDto:{}", pageDto);
        categoryService.list(pageDto);
        return new ResponseDto <PageDto<CategoryDto>>(pageDto);
    }

    @GetMapping("/all")
    @ApiOperation("获取系统类型")
    public ResponseDto <List <CategoryDto>> all() {
        ResponseDto <List <CategoryDto>> responseDto = new ResponseDto <>();
        List <CategoryDto> categoryList = categoryService.all();
        responseDto.setContent(categoryList);
        return responseDto;
    }

    @PostMapping("/save")
    @ApiOperation("保存分类")
    @ApiImplicitParam(name = "CategoryDto", value = "分类信息", required = true, dataType = "object", paramType = "json")
    public ResponseDto <CategoryDto> save(@RequestBody CategoryDto categoryDto) {
        LOG.info("categoryDto:{}", categoryDto);
        // 保存校验
        ValidatorUtil.require(categoryDto.getParent(), "父id");
        ValidatorUtil.require(categoryDto.getName(), "名称");
        ValidatorUtil.length(categoryDto.getName(), "名称", 1, 50);

        categoryService.save(categoryDto);
        return new ResponseDto <>(categoryDto);
    }

    @GetMapping("/del/{id}")
    @ApiOperation("根据 id 删除分类")
    @ApiImplicitParam(name = "id", value = "请求信息", required = true, dataType = "object", paramType = "json")
    public ResponseDto<String> del(@PathVariable String id) {
        LOG.info("id:{}", id);
        categoryService.del(id);
        return new ResponseDto <>("200", "删除成功！");
    }

}